package
{
	import org.flixel.*;
	import states.MenuState;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class LevelGrave extends Entity
	{
		public static const LOCKED:uint = 0;
		public static const UNLOCKED:uint = 1;
		public static const CLEARED:uint = 2;
		
		private static const _lockedSource:Class = Assets.ImgGraveLocked;
		private static const _unlockedSource:Class = Assets.ImgGraveUnlocked;
		private static const _clearedSource:Class = Assets.ImgGraveCleared;
		
		private var _name:FlxText;
		private var _scoreText:FlxText;
		
		public var level:Class;
		public var className:String;
		public var next:LevelGrave;
		public var previous:LevelGrave;
		public var highScore:int;
		
		public function LevelGrave(i:uint = 0, j:uint = 0)
		{
			super(i, j, false);
			
			loadGraphic(_lockedSource);
			
			state = LOCKED;
			
			_name = new FlxText(2 * Maze.tileWidth, 2 * Maze.tileHeight, 25 * Maze.tileWidth);
			_name.font = "cooper_black";
			_name.size = 20;
			_name.color = 0xFF000000;
			
			_scoreText = new FlxText(2 * Maze.tileWidth, 3 * Maze.tileHeight, 25 * Maze.tileWidth);
			_scoreText.font = "cooper_black";
			_scoreText.size = 20;
			_scoreText.color = 0xFF000000;
			_scoreText.text = "High Score: 0";
			
			level = null;
			next = null;
			previous = null;
			highScore = 0;
		}
		
		override public function destroy():void
		{
			super.destroy();
			
			_name.destroy();
			_name = null;
			_scoreText.destroy();
			_scoreText = null;
			
			level = null;
			next = null;
			previous = null;
		}
		
		override public function update():void
		{
			if (FlxG.overlap(this, Player.player))
			{
				Registry.misc.add(_name);
				Registry.misc.add(_scoreText);
				if (FlxG.keys.justPressed("SPACE"))
				{
					if (state != LOCKED)
					{
						Registry.paused = true;
						FlxG.play(Assets.SndTeleport, Registry.soundVolume);
						FlxG.fade(0xFF000000, 1, onFade);
					}
				}
			}
			else
			{
				Registry.misc.remove(_name);
				Registry.misc.remove(_scoreText);
			}
			
			say("ID");
		}
		
		public function get prettyName():String
		{
			return className + ": " + name;
		}
		
		public function get name():String
		{
			return _name.text;
		}
		
		public function set name(text:String):void
		{
			_name.text = text;
		}
		
		public function setScore(newScore:int):void
		{
			highScore = newScore;
			_scoreText.text = "Highs Score: " + highScore.toString();
		}
		
		public function setState(newState:uint):void
		{
			state = newState;
			switch (state)
			{
				case LOCKED: 
				{
					loadGraphic(_lockedSource);
					break;
				}
				case UNLOCKED: 
				{
					loadGraphic(_unlockedSource, true);
					addAnimation("glow", [0, 1, 2], 3);
					play("glow");
					break;
				}
				case CLEARED: 
				{
					loadGraphic(_clearedSource);
					break;
				}
			}
			width = 25;
			height = 25;
			centerOffsets();
		}
		
		private function onFade():void
		{
			Registry.paused = false;
			FlxG.switchState(new level());
		}
	}
}